package service

import (
	"errors"
	"fmt"
	"math/rand"
	"time"
	"webIm/model"
	"webIm/util"
)

type UserService struct {

}
func (s *UserService) Register(
	mobile,
	passwd,
	nickname,
	avatar,
	sex string)  (user model.User,err error){
	temp := model.User{}
	_, err = DbEngin.Where("mobile=?", mobile).Get(&temp)
	if err != nil {
		return temp,err
	}
	if temp.Id > 0 {
		return temp,errors.New("该手机已注册！")
	}
	temp.Mobile =mobile
	temp.Avatar = avatar
	temp.Nickname = nickname
	temp.Sex = sex
	temp.Salt = fmt.Sprintf("%06d",rand.Int31n(100000))
	temp.Passwd = util.MakePasswd(passwd,temp.Salt)

	temp.Createat = time.Now()
	_, err = DbEngin.InsertOne(&temp)

	return temp,nil
}
func (s *UserService) Login(
	mobile,
	passwd string)  (user model.User,err error){
	return user,nil
}